home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / qltk / searchbar.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  9KB  |  201 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import os
  5. from gi.repository import Gtk, GObject, GLib
  6. from quodlibet import config
  7. from quodlibet import const
  8. from quodlibet.parse import Query
  9. from quodlibet.qltk.cbes import ComboBoxEntrySave
  10. from quodlibet.qltk.ccb import ConfigCheckMenuItem
  11. from quodlibet.qltk.x import SeparatorMenuItem
  12. from quodlibet.util import limit_songs, DeferredSignal
  13.  
  14. class SearchBarBox(Gtk.HBox):
  15.     '''
  16.         A search bar widget for inputting queries.
  17.  
  18.         signals:
  19.             query-changed - a parsable query string
  20.             focus-out - If the widget gets focused while being focused
  21.                 (usually for focusing the songlist)
  22.     '''
  23.     __gsignals__ = {
  24.         'query-changed': (GObject.SignalFlags.RUN_LAST, None, (object,)),
  25.         'focus-out': (GObject.SignalFlags.RUN_LAST, None, ()) }
  26.     timeout = 400
  27.     
  28.     def __init__(self, filename = None, completion = None, accel_group = None):
  29.         super(SearchBarBox, self).__init__(spacing = 6)
  30.         if filename is None:
  31.             filename = os.path.join(const.USERDIR, 'lists', 'queries')
  32.         combo = ComboBoxEntrySave(filename, count = 8, validator = Query.is_valid_color, title = _('Saved Searches'), edit_title = _('Edit saved searches...'))
  33.         self._SearchBarBox__deferred_changed = DeferredSignal(self._SearchBarBox__filter_changed, timeout = self.timeout, owner = self)
  34.         self._SearchBarBox__combo = combo
  35.         entry = combo.get_child()
  36.         self._SearchBarBox__entry = entry
  37.         if completion:
  38.             entry.set_completion(completion)
  39.         self._SearchBarBox__sig = combo.connect('text-changed', self._SearchBarBox__text_changed)
  40.         entry.connect('clear', self._SearchBarBox__filter_changed)
  41.         entry.connect('backspace', self._SearchBarBox__text_changed)
  42.         entry.connect('populate-popup', self._SearchBarBox__menu)
  43.         entry.connect('activate', self._SearchBarBox__filter_changed)
  44.         entry.connect('activate', self._SearchBarBox__save_search)
  45.         entry.connect('focus-out-event', self._SearchBarBox__save_search)
  46.         entry.set_placeholder_text(_('Search'))
  47.         entry.set_tooltip_text(_('Search your library, using free text or QL queries'))
  48.         combo.enable_clear_button()
  49.         self.pack_start(combo, True, True, 0)
  50.         if accel_group:
  51.             (key, mod) = Gtk.accelerator_parse('<ctrl>L')
  52.             accel_group.connect(key, mod, (0,), (lambda : entry.mnemonic_activate(True)))
  53.         for child in self.get_children():
  54.             child.show_all()
  55.         
  56.  
  57.     
  58.     def set_text(self, text):
  59.         '''Set the text without firing any signals'''
  60.         self._SearchBarBox__deferred_changed.abort()
  61.         self._SearchBarBox__inhibit()
  62.         self._SearchBarBox__entry.set_text(text)
  63.         self._SearchBarBox__uninhibit()
  64.  
  65.     
  66.     def get_text(self):
  67.         '''Get the active text as unicode'''
  68.         return self._SearchBarBox__entry.get_text().decode('utf-8')
  69.  
  70.     
  71.     def changed(self):
  72.         '''Triggers a filter-changed signal if the current text
  73.         is a parsable query
  74.         '''
  75.         self._SearchBarBox__filter_changed()
  76.  
  77.     
  78.     def __inhibit(self):
  79.         self._SearchBarBox__combo.handler_block(self._SearchBarBox__sig)
  80.  
  81.     
  82.     def __uninhibit(self):
  83.         self._SearchBarBox__combo.handler_unblock(self._SearchBarBox__sig)
  84.  
  85.     
  86.     def __menu(self, entry, menu):
  87.         sep = SeparatorMenuItem()
  88.         sep.show()
  89.         menu.prepend(sep)
  90.         cb = ConfigCheckMenuItem(_('Search after _typing'), 'settings', 'eager_search', populate = True)
  91.         cb.set_tooltip_text(_('Show search results after the user stops typing.'))
  92.         cb.show()
  93.         menu.prepend(cb)
  94.  
  95.     
  96.     def __mnemonic_activate(self, label, group_cycling):
  97.         widget = label.get_mnemonic_widget()
  98.         if widget.is_focus():
  99.             self.emit('focus-out')
  100.             return True
  101.  
  102.     
  103.     def __save_search(self, entry, *args):
  104.         if args and not config.getboolean('settings', 'eager_search'):
  105.             return None
  106.         text = None.get_text().strip()
  107.         if text and Query.is_parsable(text):
  108.             self._SearchBarBox__inhibit()
  109.             self._SearchBarBox__combo.prepend_text(text)
  110.             self._SearchBarBox__combo.write()
  111.             self._SearchBarBox__uninhibit()
  112.  
  113.     
  114.     def __filter_changed(self, *args):
  115.         self._SearchBarBox__deferred_changed.abort()
  116.         text = self.get_text()
  117.         if Query.is_parsable(text):
  118.             GLib.idle_add(self.emit, 'query-changed', text)
  119.  
  120.     
  121.     def __text_changed(self, *args):
  122.         if self._SearchBarBox__combo.get_active() != -1:
  123.             self._SearchBarBox__filter_changed()
  124.             return None
  125.         if not None.getboolean('settings', 'eager_search'):
  126.             return None
  127.         None._SearchBarBox__deferred_changed()
  128.  
  129.  
  130.  
  131. class LimitSearchBarBox(SearchBarBox):
  132.     '''A version of `SearchBarBox` that allows specifying the limiting and
  133.     weighting of a search.'''
  134.     
  135.     class Limit(Gtk.HBox):
  136.         __gsignals__ = {
  137.             'changed': (GObject.SignalFlags.RUN_LAST, None, ()) }
  138.         
  139.         def __init__(self):
  140.             super(LimitSearchBarBox.Limit, self).__init__(spacing = 3)
  141.             label = Gtk.Label(label = _('_Limit:'))
  142.             self.pack_start(label, True, True, 0)
  143.             self._Limit__limit = limit = Gtk.SpinButton()
  144.             self._Limit__limit.connect('value-changed', self._Limit__changed)
  145.             limit.set_numeric(True)
  146.             limit.set_range(0, 9999)
  147.             limit.set_increments(5, 100)
  148.             label.set_mnemonic_widget(limit)
  149.             label.set_use_underline(True)
  150.             self.pack_start(limit, True, True, 0)
  151.             self._Limit__weight = Gtk.CheckButton(label = _('_Weight'), use_underline = True)
  152.             self._Limit__weight.connect('toggled', self._Limit__changed)
  153.             self.pack_start(self._Limit__weight, True, True, 0)
  154.             for child in self.get_children():
  155.                 child.show()
  156.             
  157.  
  158.         
  159.         def __changed(self, *args):
  160.             self.emit('changed')
  161.  
  162.         
  163.         def value(self):
  164.             return self._Limit__limit.get_value_as_int()
  165.  
  166.         value = property(value)
  167.         
  168.         def weighted(self):
  169.             return self._Limit__weight.get_active()
  170.  
  171.         weighted = property(weighted)
  172.  
  173.     
  174.     def __init__(self, show_limit = False, *args, **kwargs):
  175.         super(LimitSearchBarBox, self).__init__(*args, **kwargs)
  176.         self._LimitSearchBarBox__limit = self.Limit()
  177.         self.pack_start(self._LimitSearchBarBox__limit, False, True, 0)
  178.         self._LimitSearchBarBox__limit.set_no_show_all(not show_limit)
  179.         self._LimitSearchBarBox__limit.connect('changed', self._LimitSearchBarBox__limit_changed)
  180.  
  181.     
  182.     def __limit_changed(self, *args):
  183.         self.changed()
  184.  
  185.     
  186.     def limit(self, songs):
  187.         if self._LimitSearchBarBox__limit.get_visible():
  188.             return limit_songs(songs, self._LimitSearchBarBox__limit.value, self._LimitSearchBarBox__limit.weighted)
  189.         return None
  190.  
  191.     
  192.     def toggle_limit_widgets(self, button):
  193.         '''Toggles the visibility of the limit widget according to `button`'''
  194.         if button.get_active():
  195.             self._LimitSearchBarBox__limit.show()
  196.         else:
  197.             self._LimitSearchBarBox__limit.hide()
  198.         self.changed()
  199.  
  200.  
  201.